"""
author: yzh
date: 2021/1/21
"""
import requests

from api.getKey import getkeyList
from api.jwxtLogin import jwxtlogin,iPlanetDirectoryPro
from api.getOldJsessionID import old_JSESSIONID

from resources.NP import username,password

JWXTIndexUrl = 'https://jwxt-ecut-edu-cn-18801.webvpn1.ecit.cn/caslogin.jsp'
MainFrameUrl = 'https://jwxt-ecut-edu-cn-18801.webvpn1.ecit.cn/menu/mainFrame.jsp'
MenuUrl = 'https://jwxt-ecut-edu-cn-18801.webvpn1.ecit.cn/menu/menu.jsp'
GradeLnAllActionUrl = 'https://jwxt-ecut-edu-cn-18801.webvpn1.ecit.cn/gradeLnAllAction.do?type=ln&oper=qb'
QueryGradeUrl = 'https://jwxt-ecut-edu-cn-18801.webvpn1.ecit.cn/gradeLnAllAction.do?type=ln&oper=qbinfo&lnxndm=2020-2021%D1%A7%C4%EA%B5%DA%D2%BB%D1%A7%C6%DA(%C1%BD%D1%A7%C6%DA)'

def querygrade():

    jwxtlogin(username, password)

    UserAgent = "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 4 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19"
    Cookie = getkeyList[1] + getkeyList[0] + old_JSESSIONID[0] + iPlanetDirectoryPro[0]

    # 进入ticket
    url = iPlanetDirectoryPro[1]
    TicketHeaders = {"User-Agent": UserAgent,"Referer": "https://authserver-ecut-edu-cn-443.webvpn1.ecit.cn/","Cookie": Cookie}
    requests.get(url, headers=TicketHeaders, allow_redirects=False)

    # 进入到主页
    JWXTIndexHeaders = {"User-Agent": UserAgent,"Referer": "https://authserver-ecut-edu-cn-443.webvpn1.ecit.cn/","Cookie": Cookie}
    requests.get(url=JWXTIndexUrl, headers=JWXTIndexHeaders)

    # 进入到caslogin
    MainFrameHeaders = {"User-Agent": UserAgent,"Referer": "https://jwxt-ecut-edu-cn-18801.webvpn1.ecit.cn/caslogin.jsp","Cookie": Cookie}
    requests.get(url=MainFrameUrl, headers=MainFrameHeaders)

    # 进入到mainFrame
    MenuHeaders = {"User-Agent": UserAgent,"Referer": "https://jwxt-ecut-edu-cn-18801.webvpn1.ecit.cn/menu/mainFrame.jsp","Cookie": Cookie}
    requests.get(url=MenuUrl, headers=MenuHeaders)

    # 进入到menu
    GradeLnAllActionHeaders = {"User-Agent": UserAgent,"Referer": "https://jwxt-ecut-edu-cn-18801.webvpn1.ecit.cn/menu/menu.jsp","Cookie": Cookie}
    requests.get(url=GradeLnAllActionUrl, headers=GradeLnAllActionHeaders)

    # 查询成绩
    QueryGradeHeaders = {"User-Agent": UserAgent,"Referer": "https://jwxt-ecut-edu-cn-18801.webvpn1.ecit.cn/gradeLnAllAction.do?type=ln&oper=qb","Cookie": Cookie}
    response = requests.get(url=QueryGradeUrl, headers=QueryGradeHeaders)
    return response.text